package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamEmployee;
import cn.wolfcode.enums.QuestionTypeEnum;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamEmployeeService;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IExamEmployeeService examEmployeeService;


    @RequiredPermission(name="试卷页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }


    @RequiredPermission(name="试卷删除",expression="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Exam exam) {
        if (exam.getId() == null) {
            examService.save(exam);
        } else {
            examService.update(exam);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑跳转",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
        }
        return "exam/input";
    }

    @RequiredPermission(name="试卷预览",expression="exam:show")
    @RequestMapping("/show")
    public String show(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
            model.addAttribute("radioList", questionService.queryByQuestionList(id, QuestionTypeEnum.RADIO.getValue()));
            model.addAttribute("checkboxList", questionService.queryByQuestionList(id, QuestionTypeEnum.CHECKBOX.getValue()));
            model.addAttribute("judgeList", questionService.queryByQuestionList(id, QuestionTypeEnum.JUDGE.getValue()));
        }
        return "exam/show";
    }

    @RequestMapping("/answer")
    public String answer(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
            model.addAttribute("radioList", questionService.queryByQuestionList(id, QuestionTypeEnum.RADIO.getValue()));
            model.addAttribute("checkboxList", questionService.queryByQuestionList(id, QuestionTypeEnum.CHECKBOX.getValue()));
            model.addAttribute("judgeList", questionService.queryByQuestionList(id, QuestionTypeEnum.JUDGE.getValue()));
        }
        return "exam/answer";
    }

    @RequiredPermission(name="试卷生成",expression="exam:createExam")
    @RequestMapping("/createExam")
    @ResponseBody
    public JsonResult createExam(Exam exam) {
        examService.createExam(exam);
        return new JsonResult();
    }

    @RequestMapping("/commitExam")
    @ResponseBody
    public JsonResult commitExam(ExamEmployee examEmployee) {
        examEmployeeService.commitExam(examEmployee);
        return new JsonResult();
    }
}
